package com.sunday.aomi.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunday.common.base.BaseFragment;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.widgets.ptr.PtrClassicFrameLayout;
import com.sunday.common.widgets.ptr.PtrDefaultHandler;
import com.sunday.common.widgets.ptr.PtrFrameLayout;
import com.sunday.common.widgets.ptr.PtrHandler;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.aomi.R;
import com.sunday.aomi.adapter.IndexAdapter;
import com.sunday.aomi.base.BaseApp;
import com.sunday.aomi.ui.product.ProductClassifyActivity;
import com.sunday.member.entity.news.News;
import com.sunday.member.entity.product.Product;
import com.sunday.member.entity.product.ProductCategory;
import com.sunday.member.http.MemberClient;
import com.sunday.member.entity.IndexData;
import com.sunday.member.utils.SignUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/5/22.
 */

public class IndexFragment extends BaseFragment {


    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.left_txt)
    TextView leftTxt;
    @Bind(R.id.right_menu)
    ImageView rightMenu;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.ptr_frame)
    PtrClassicFrameLayout ptrFrame;

    private IndexAdapter adapter;
    private LinearLayoutManager layoutManager;
    private long memberId;


    public static IndexFragment newInstance() {
        IndexFragment fragment = new IndexFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_index, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        memberId= BaseApp.getInstance().getMember().getId();
        initParams();
        getData();
    }

    private void initView(){
        title.setText("奥密果蔬");
        rightMenu.setVisibility(View.VISIBLE);
        handlerRecyclerView();

    }

    private List<ProductCategory> categoryList=new ArrayList<>();
    private List<Product> productList=new ArrayList<>();
    private List<News> newsList=new ArrayList<>();
    private List<News> imgList=new ArrayList<>();
    private void handlerRecyclerView(){
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new IndexAdapter(mContext,imgList,newsList,productList,categoryList);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .drawable(R.drawable.shape_divider)
                .build());
        ptrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getData();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, recyclerView, header);
            }
        });
    }

    private void initParams(){}

    private void getData(){
        showLoadingDialog(0);
        String nonce= MD5Utils.random();//随机数
        String[] str1=new String[]{"nonce"};
        String[] str2=new String[]{nonce};
        Call<ResultDO<IndexData>> call= MemberClient.getApiAdapter().getIndexData(null,nonce,SignUtil.getSign(str1,str2));
        call.enqueue(new Callback<ResultDO<IndexData>>() {
            @Override
            public void onResponse(Call<ResultDO<IndexData>> call, Response<ResultDO<IndexData>> response) {
                if (isFinish||response.body()==null){return;}
                dissMissDialog();
                ptrFrame.refreshComplete();
                ResultDO<IndexData> resultDO=response.body();
                if (resultDO.getCode()==0){
                    if (resultDO.getResult()==null){return;}
                    if (resultDO.getResult().getCategories()!=null&&!resultDO.getResult().getCategories().isEmpty()){
                        categoryList.clear();
                        categoryList.addAll(resultDO.getResult().getCategories());
                    }
                    if (resultDO.getResult().getProducts()!=null&&!resultDO.getResult().getProducts().isEmpty()){
                        productList.clear();
                        productList.addAll(resultDO.getResult().getProducts());
                    }
                    if (resultDO.getResult().getImages()!=null&&!resultDO.getResult().getImages().isEmpty()){
                        imgList.clear();
                        imgList.addAll(resultDO.getResult().getImages());
                    }

                    if (resultDO.getResult().getNews()!=null&&!resultDO.getResult().getNews().isEmpty()){
                        newsList.clear();
                        newsList.addAll(resultDO.getResult().getNews());
                    }
                    adapter.notifyDataSetChanged();
                    updateView();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<IndexData>> call, Throwable t) {
                ptrFrame.refreshComplete();
            }
        });
    }

    @OnClick({R.id.right_menu})
    void onClick(View v){
        switch (v.getId()){
            case R.id.right_menu:
                intent=new Intent(mContext, ProductClassifyActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void updateView(){

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


}
